package de.betagames.dreamworld.main;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import de.betagames.dreamworld.charactereditor.view.CharacterEditor;

public class Main extends JFrame {

	private JPanel top, bottom;
	private JRadioButton character, world, play, item;

	public static void main(String[] args) {
		new Main();
	}

	public Main() {
		setTitle("DreamWorld - Start");
		setSize(680, 480);
		setLocationRelativeTo(null);
		setResizable(false);
		setLayout(new BorderLayout());
		initBottom();
		initTop();
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setVisible(true);
	}

	private void initBottom() {

		bottom = new JPanel();
		bottom.setLayout(new BorderLayout());

		JPanel botBottom = new JPanel();
		botBottom.setLayout(new GridLayout(1, 2));
		JButton ok = new JButton("Start Dreaming");
		ok.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				onward();
			}
		});
		JButton cancel = new JButton("Stay Awake");
		cancel.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				retreat();
			}
		});
		botBottom.add(ok);
		botBottom.add(cancel);
		bottom.add(botBottom, BorderLayout.SOUTH);

		JPanel topBottom = new JPanel();
		ButtonGroup bg = new ButtonGroup();

		topBottom.setLayout(new GridLayout(2, 2));

		play = new JRadioButton("play the sick game of the Weaver");
		bg.add(play);
		topBottom.add(play);
		character = new JRadioButton(
				"redream your Dream-Ego or dream another one");
		bg.add(character);
		topBottom.add(character);
		world = new JRadioButton("redream your world or dream another one");
		bg.add(world);
		topBottom.add(world);
		item = new JRadioButton(
				"redream some items in your world or dream new ones");
		bg.add(item);
		topBottom.add(item);
		bottom.add(topBottom, BorderLayout.CENTER);
		add(bottom, BorderLayout.SOUTH);

	}

	private void onward() {

		if (play.isSelected()) {
			System.out.println("SPIEL SOLLTE STARTEN");
		} else if (world.isSelected()) {
			System.out.println("WELTEDITOR SOLLTE STARTEN");
		} else if (character.isSelected()) {
			new CharacterEditor();
			this.dispose();
		} else if (item.isSelected()) {
			System.out.println("ITEMGENERATOR SOLLTE STARTEN");
		}

	}

	private void retreat() {

		Object[] options = { "Yes, please", "No, thanks" };
		int n = JOptionPane
				.showOptionDialog(
						this,
						"<html>Do you really want to retreat?<br>"
								+ "Do you really want to abandon all hope of the people "
								+ "trapped in their dreams?</html>",
						"Retreat?", JOptionPane.YES_NO_OPTION,
						JOptionPane.QUESTION_MESSAGE, null, options, options[1]);

		if (n == JOptionPane.YES_OPTION) {
			System.exit(0);
		}

	}

	private void initTop() {

		top = new JPanel();

		top.setBackground(Color.BLACK);
		top.setOpaque(true);
		add(top, BorderLayout.CENTER);

	}

}
